from flask import Flask
from flask_login import LoginManager
from flasgger import Swagger

login_manager = LoginManager()
swagger = Swagger()


def create_app():
    app = Flask(__name__)
    app.secret_key = 'your-secret-key-here'

    # 初始化组件
    login_manager.init_app(app)
    swagger.init_app(app)

    # 注册蓝图
    from .routes.auth import auth_bp
    from .routes.movies import movie_bp
    app.register_blueprint(auth_bp, url_prefix='/api')
    app.register_blueprint(movie_bp, url_prefix='/api')

    return app


@login_manager.user_loader
def load_user(user_id):
    from app.repositories.user_repo import UserRepository
    return UserRepository().find_by_id(int(user_id))
